package cc.vv.btpartyjob.partymanager.fragment

import android.support.v4.widget.SwipeRefreshLayout
import android.view.View
import cc.vv.baselibrary.bean.base.BaseResponseObj
import cc.vv.baselibrary.http.BaseHttpCallBack
import cc.vv.baselibrary.util.UserInfoManageUtil
import cc.vv.baselibrary.view.BTLoadMoreView
import cc.vv.btpartyjob.partymanager.R
import cc.vv.btpartyjob.partymanager.adapter.RegisterUnitStatisticsAdapter
import cc.vv.btpartyjob.partymanager.api.PartyManagerAPI
import cc.vv.btpartyjob.partymanager.delegate.ManagerBaseBinder
import cc.vv.btpartyjob.partymanager.delegate.RegisterStaticsUnitFragmentDelegate
import cc.vv.btpartyjob.partymanager.moudel.bean.ReportStatisticsBean
import cc.vv.btpartyjob.partymanager.moudel.req.ReportStaticsRequestObj
import cc.vv.btpartyjob.partymanager.moudel.resp.ReportStaticUnitResponseObj
import cc.vv.btpartyjob.partymanager.server.PartyManagerServer
import cc.vv.lkbasecomponent.http.LKHttp
import cc.vv.mvp.binder.BaseDataBinder
import cc.vv.mvp.fragment.BaseFragmentMVP
import com.chad.library.adapter.base.BaseQuickAdapter

/**
 * 单位报到统计fragment
 * Created by Chen.qi on 2018/10/27.
 */
class RegisterUnitStaticsFragment : BaseFragmentMVP<RegisterStaticsUnitFragmentDelegate>(),
        BaseQuickAdapter.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener {

    private var whetherRefresh = false

    /**
     * 街道Id
     */
    private var streetId: String? = null

    /**
     * 社区id
     */
    private var communityId: String? = null


    /**
     * 适配器
     */
    private var mAdapter: RegisterUnitStatisticsAdapter? = null

    /**
     * 集合数据
     */
    private var mDataList: ArrayList<ReportStatisticsBean>? = null


    override fun initData() {
        if (mAdapter == null) {
            mDataList = ArrayList()
            mAdapter = RegisterUnitStatisticsAdapter((R.layout.item_register_statistics_unit))
            viewDelegate?.setRecyclerViewAdapter(mAdapter)
            mAdapter?.setLoadMoreView(BTLoadMoreView())
            viewDelegate?.setAdapterData(mAdapter, mDataList)
        }

    }

    override fun onVisibilityChanged(visible: Boolean) {
        super.onVisibilityChanged(visible)
        if (visible && !whetherRefresh) {
            val userInfo = UserInfoManageUtil.getUserInfo()
            val userOrgList = userInfo?.userOrgList
            if (null != userOrgList) {
                for (i in userOrgList.indices) {
                    when {
                        userOrgList[i].type == 1 -> streetId = userOrgList[i].id
                        userOrgList[i].type == 2 -> communityId = userOrgList[i].id
                    }
                }
            }
            PartyManagerServer.unitReportStatice(streetId, communityId, true)
            whetherRefresh = true
        }
    }

    override fun bindEvenListener() {
        viewDelegate?.bindRefreshListener(this)

    }

    override fun getDelegateClass(): Class<RegisterStaticsUnitFragmentDelegate> {
        return RegisterStaticsUnitFragmentDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return ManagerBaseBinder()
    }


    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
    }


    override fun getData(obj: BaseResponseObj<*>?) {
        super.getData(obj)
        if (whetherRefresh) {
            when (obj) {
                is ReportStaticUnitResponseObj -> {
                    viewDelegate?.stopRefreshing()
                    mDataList?.clear()
                    if (null != obj.data) {
                        mDataList!!.addAll(obj.data)
                    }
                    viewDelegate?.setAdapterData(mAdapter, mDataList)
                }
            }
        }
    }

    override fun onRefresh() {
        PartyManagerServer.unitReportStatice(streetId, communityId, true)
    }

    override fun getBadCode(obj: BaseResponseObj<*>?) {
        super.getBadCode(obj)
        viewDelegate?.stopRefreshing()
    }

    override fun onHttpFailure(url: String?, exceptionStr: String?) {
        super.onHttpFailure(url, exceptionStr)
        viewDelegate?.stopRefreshing()
    }


    /**
     * 组织选择后
     */
    fun unitStaticsHttp(orgId: String?, type: String) {
        streetId = ""
        communityId = ""
        when (type) {
            "0" -> {
            }
            "1" -> streetId = orgId
            "2" -> communityId = orgId
        }
        val request = ReportStaticsRequestObj()
        request.street = streetId
        request.community = communityId
        LKHttp.get(PartyManagerAPI.unitReportStaticsUrl, request, ReportStaticUnitResponseObj::class.java,
                object : BaseHttpCallBack<ReportStaticUnitResponseObj>() {
                    override fun onSuccess(url: String?, obj: ReportStaticUnitResponseObj?) {
                        super.onSuccess(url, obj)
                        mDataList?.clear()
                        if (null != obj?.data) {
                            mDataList!!.addAll(obj.data)
                        }
                        viewDelegate?.setAdapterData(mAdapter, mDataList)
                    }

                }, false)
    }

}